//
//  ApplyCreditViewController.m
//  Vest_native
//
//  Created by WangZhenyu on 16/12/15.
//  Copyright © 2016年 WangZhenyu. All rights reserved.
//

#import "ApplyCreditViewController.h"
#import "WebShowViewController.h"
#import "LoadConfig.h"
#import "NoContentView.h"
#import "AppleCreditTableViewCell.h"

@interface ApplyCreditViewController (){
    NSString                *creditUrl;
    NSArray                 *contentArr;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ApplyCreditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"信用卡申请";
    
    [self addBackBtn];
    [((UIButton *)[self addRigthBtn:@"办卡进度" withColor:nil]) addTarget:self action:@selector(bankajinduPress) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.tableFooterView = [UIView new];
    
    NSDictionary *configDic = [[LoadConfig shareStance] getBankajindu];
    if (configDic.count > 0) {
        creditUrl = [configDic objectForKey:@"bankajindu"];
    }
    [[LoadConfig shareStance] loadBankajinduSuccess:^(NSDictionary *dic) {
        creditUrl = [dic objectForKey:@"bankajindu"];
    }];
    
    [self requestContent:YES];
    
    [self.tableView setFrame:CGRectMake(0, 64 + IS_iPhoneX_Top, SCREEN_WIDTH_DEVICE, SCREEN_HEIGHT_DEVICE - 64 - IS_iPhoneX_Top)];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//请求数据
- (void)requestContent:(BOOL)alert{
    if (alert) {
        [self loadingHUDAlert:@"正在加载"];
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:[NSNumber numberWithInteger:0] forKey:@"page"];
    [dic setObject:[NSNumber numberWithInteger:100] forKey:@"size"];
    
    [[ServiceRequest sharedService] GET:@"creditcardapplys" parameters:dic success:^(id responseObject) {
        [self hideHudAlert];
        contentArr = responseObject;
        if (contentArr.count == 0) {
            self.tableView.tableHeaderView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageType:InfoImageType_Default title:@"暂无信用卡产品"];
        }else{
            self.tableView.tableHeaderView = nil;
        }
        [self.tableView reloadData];
        
    } failure:^(NSString *error, NSInteger code) {
        [self hideHudAlert];
        if (code == 0) {
            self.tableView.tableHeaderView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageType:InfoImageType_NoNetWork title:error];
        }else if (code == 404){
            self.tableView.tableHeaderView = [[NoContentView alloc] initItemFrame:self.tableView.frame imageType:InfoImageType_Default title:@"暂无信用卡产品"];
            [self.tableView reloadData];
        }else{
            [self showHUDAlert:error];
        }
    }];
}


- (void)bankajinduPress{
    if (creditUrl.length > 0) {
        WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
        viewCon.urlStr = creditUrl;
        [self.navigationController pushViewController:viewCon animated:YES];
    }else{
        [[LoadConfig shareStance] loadBankajinduSuccess:^(NSDictionary *dic) {
            creditUrl = [dic objectForKey:@"bankajindu"];
            WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
            viewCon.urlStr = creditUrl;
            [self.navigationController pushViewController:viewCon animated:YES];
        }];
    }
}

#pragma mark - UITableView datasource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [contentArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"AppleCreditCell";
    AppleCreditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"AppleCreditTableViewCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.titleLabel.text = [[contentArr objectAtIndex:indexPath.row] objectForKey:@"title"];
    cell.desLabel.text = [[contentArr objectAtIndex:indexPath.row] objectForKey:@"description"];
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:[[contentArr objectAtIndex:indexPath.row] objectForKey:@"icon"]]];
    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    WebShowViewController *viewCon = [[WebShowViewController alloc] initWithNibName:@"WebShowViewController" bundle:nil];
    viewCon.urlStr = [[contentArr objectAtIndex:indexPath.row] objectForKey:@"url"];
    [self.navigationController pushViewController:viewCon animated:YES];
}


@end
